package com.nari.ncps.screen.service.impl.dryport;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.dryport.DryportProdoperOwnCoal;
import com.nari.ncps.screen.mapper.dryport.DryportProdoperOwnCoalMapper;
import com.nari.ncps.screen.service.dryport.DryportProdoperOwnCoalService;
import com.nari.ncps.screen.vo.dryport.OwnCoalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 自有煤炭完成量 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class DryportProdoperOwnCoalServiceImpl extends ServiceImpl<DryportProdoperOwnCoalMapper, DryportProdoperOwnCoal> implements DryportProdoperOwnCoalService {

    @Autowired
    private DryportProdoperOwnCoalMapper ownCoalMapper;

    @Autowired
    private DryportProdoperOwnCoalMapper dryportProdoperOwnCoalMapper;

    @Override
    public List<DryportProdoperOwnCoal> getMonthlyStatsByYear(Integer year) {
        return ownCoalMapper.getMonthlyStatsByYear(year);
    }

    @Override
    public List<DryportProdoperOwnCoal> getLatestMonthlyStats() {
        return ownCoalMapper.getLatestMonthlyStats();
    }

    @Override
    public DryportProdoperOwnCoal getStatsByYearAndMonth(Integer year, Integer month) {
        return ownCoalMapper.getStatsByYearAndMonth(year, month);
    }
    
    @Override
    public List<OwnCoalVO> getOwnCoalStatistics(Integer year) {

        List<OwnCoalVO> voList = new ArrayList<>();
        // 获取当前日期和6个月前的日期
        LocalDate currentDate = LocalDate.now();
        LocalDate sixMonthsAgo = currentDate.minusMonths(5); // 包含当前月，所以减5个月

        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String startDateStr = sixMonthsAgo.format(formatter);
        String endDateStr = currentDate.format(formatter);

        System.out.println("查询日期范围: " + startDateStr + " - " + endDateStr);

        // 构建查询条件
        LambdaQueryWrapper<DryportProdoperOwnCoal> lqw = new LambdaQueryWrapper<>();

        lqw.eq(DryportProdoperOwnCoal::getOrgCode, "100360")
                .eq(DryportProdoperOwnCoal::getDelFlag, 0)
                .eq(DryportProdoperOwnCoal::getDateType,1)
                .ge(DryportProdoperOwnCoal::getStatisticTime, startDateStr)
                .le(DryportProdoperOwnCoal::getStatisticTime, endDateStr);

        // 执行查询
        List<DryportProdoperOwnCoal> entityList = dryportProdoperOwnCoalMapper.selectList(lqw);

        // 将实体转换为VO
        if (entityList != null && !entityList.isEmpty()) {
            for(DryportProdoperOwnCoal entity : entityList) {
                OwnCoalVO vo = new OwnCoalVO();
                BeanUtil.copyProperties(entity, vo);
                vo.setOwnCoalPlanned(entity.getOwnCoalPlanned());
                vo.setOwnCoalCompleted(entity.getOwnCoalCompleted() == null ? BigDecimal.ZERO :
                        entity.getOwnCoalCompleted().divide(new BigDecimal(10000))
                                .setScale(2, BigDecimal.ROUND_HALF_UP));
                vo.setOwnCompletionRate(entity.getOwnCompletionRate());

                if(entity.getNonCoalCompleted()!=null && entity.getNonCoalPlanned()!=null
                        && entity.getNonCoalPlanned().compareTo(BigDecimal.ZERO)>0) {
                    vo.setOwnCompletionRate(entity.getNonCoalCompleted().divide(new BigDecimal(10000))
                            .divide(entity.getNonCoalPlanned()).setScale(2, BigDecimal.ROUND_HALF_UP));
                }

                voList.add(vo);
            }
        }

        List<OwnCoalVO> sortedList = voList.stream()
                .sorted(Comparator.comparing(OwnCoalVO::getYear)
                        .thenComparing(OwnCoalVO::getMonth))
                .collect(Collectors.toList());

        return sortedList;


//        // 获取最近6个月的Entity数据
//        List<DryportProdoperOwnCoal> entityList = getLatestMonthlyStats();
//
//        // 转换为VO对象
//        List<OwnCoalVO> data = new ArrayList<>();
//        if (entityList != null && !entityList.isEmpty()) {
//            for (DryportProdoperOwnCoal entity : entityList) {
//                OwnCoalVO vo = new OwnCoalVO();
//                vo.setYear(entity.getYear());
//                vo.setMonth(entity.getMonth());
//                vo.setDate(entity.getDate());
//                vo.setOwnCoalPlanned(entity.getOwnCoalPlanned());
//                vo.setOwnCoalCompleted(entity.getOwnCoalCompleted());
//                vo.setOwnCompletionRate(entity.getOwnCompletionRate());
//                data.add(vo);
//            }
//        }
//
//        return data;
    }
}
